package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.constant.RedisMessageConstant;
import com.dao.MemberDao;
import com.dao.OrderDao;
import com.dao.OrderSettingDao;
import com.dao.SetmealDao;
import com.entity.Result;
import com.pojo.Member;
import com.pojo.Order;
import com.pojo.OrderSetting;
import com.pojo.Setmeal;
import com.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImple implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    /*根据id获取套餐信息*/
    public Result findSetmealById(int id) {
        //获取信息,前台有使用到插值表达式,获取信息以后判断不能为空
        Setmeal setmealById = setmealDao.findSetmealById(id);
        return new Result(true,"套餐信息查询成功",setmealById);
    }

    /*提交预约*/
    public Result submitOrder(Map map) {
        //首先map不能为空
        if (map==null){
            return new Result(false, "预约失败");
        }
        //然后做手机号吗校验
        String telephone = (String) map.get("telephone");
        /*校验手机号是否正确*/
        if (telephone==null||"".equals(telephone)){
            return new Result(false,"手机号不能为空");
        }
        String s = "^[1][3578]\\d{9}$";
        boolean b = telephone.matches(s);
        if (!b){
            return new Result(false,"手机号输入格式错误");
        }
        //然后做验证码校验
        String service_Code = jedisPool.getResource().get(telephone + RedisMessageConstant.SENDTYPE_ORDER);
        //取出验证码
        String code = (String)map.get("validateCode");
        if (code==null||"".equals(code)){
            return new Result(false, "验证码错误");
        }
        if (service_Code==null||"".equals(service_Code)){
            return new Result(false, "你本次提交的手机号与发送验证码的手机号不是同一个");
        }
        //验证码校验
        if (!service_Code.equals(code)){
            return new Result(false,"验证码错误");
        }
        //对预约人姓名校验
        String name = (String) map.get("name");
        String s2="^[\\u2000-\\u206f\\u3000-\\u303f\\u4e00-\\u9fef\\uff00-\\uffef]*$";
        if (name==null||"".equals(name)){
            return new Result(false,"姓名不能为空");
        }
        if (!name.matches(s2)||name.length()>=5){
            return new Result(false,"姓名格式不正确,或长度太长");
        }
        //对身份证校验
        String idCard = (String) map.get("idCard");
        if (idCard==null||"".equals(idCard)){
            return new Result(false,"身份证不能为空");
        }
        String s3="^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
        if (!idCard.matches(s3)){
            return new Result(false,"身份证格式错误");
        }
        //预约日期校验
        String orderDate = (String) map.get("orderDate");
        if (orderDate==null||"".equals(orderDate)){
            return new Result(false,"日期不能为空");
        }
        /*开始数据库交互
        * 传递过来的参数有哪些?  name,sex, telephone,idCard,orderDate,setmealId,
        * 业务情况:选择日期上班,且可预约,需要有会员id,不能重复预约,
        *操作表格:补全数据预约类型,未到诊,并执行插入
        * */
        //判断传递过来的日期是否上班
        String datee=(String)map.get("orderDate");
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simple.parse(datee);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int i = orderSettingDao.findByOrderDate(date);
        if (i==0){
            //说明不上班
            return new Result(false,"该预约日期不上班");
        }
        //判断预约日期是否还有余额
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String newdate = simpleDateFormat.format(date);
        //TODO 此处可能哟错误需要String d="%"+date+"%";
        List<OrderSetting> orderSettingList = orderSettingDao.findByYearAndMonth(newdate);
        OrderSetting orderSetting = orderSettingList.get(0);
        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations>=number){
            //说明已经约满了
            return new Result(false,"该预约日期已经预约满了");
        }
        //补全信息
        //判断是否已经是我们的会员了
        Member member_=memberDao.findMemberByTelephone(telephone);
        int finalid;
        if (member_==null){
            //说明还不是我们的会员,需要给他注册个会员
            Member member=new Member();
            member.setName(name);
            member.setSex((String)map.get("sex"));
            member.setPhoneNumber(telephone);
            member.setIdCard(idCard);
            memberDao.addMember(member);
            Integer id = member.getId();
            //补全信息

            Order order = new Order(id, date, "微信预约", "未到诊", (Integer) map.get("setmealId"));
            //开始插入新增一条预约信息(上面检查通过)
                orderDao.addOrder(order);
                finalid=order.getId();
        }else {
            //说明是会员,我么需要判断该会员在这一天有没有预约过(上面检查通过)
            Integer mid = member_.getId();
            int count=orderDao.findCount(mid,date);
            if (count==0){
                //说明没有添加过,那就添加一份

                Order order=new Order(mid,date,"微信预约", "未到诊",Integer.parseInt((String) map.get("setmealId")));
                orderDao.addOrder(order);
                finalid=order.getId();
            }else {
                //说明已经添加过了
                return new Result(false,"你当天已经预约过了");
            }

        }
        //修改预约设置订单+1(上面检查通过)
        orderSettingDao.updateReservations(date,reservations+1);
        return new Result(true,"预约成功",finalid);
    }

    //体检人,体检套餐,体检日期,预约类型
    public Result findById(int id) {
        Map map=orderDao.findById(id);
        Date orderDate = (Date )map.get("orderDate");
        map.remove("orderDate");
        System.out.println(orderDate);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(orderDate);
        map.put("orderDate",format);
        return new Result(true,"查询成功",map);
    }
}
